package com.ama.controller;


import com.ama.pojo.Staff;
import com.ama.service.StaffService;
import com.ama.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


@Controller
public class P_UpdateInfoController {
    @Autowired
    StaffService staffService;

    @RequestMapping("/p_updateStaffInfo")
    public String updateStaffInfo(HttpServletRequest request, HttpSession session) {
        //用来设置和保存员工的新信息
        Staff staff = new Staff();

        Long staffId = Long.valueOf(request.getParameter("staffId"));
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String education = request.getParameter("education");
        String identity = request.getParameter("identity");
        String bankcard = request.getParameter("bankcard");
        String address = request.getParameter("address");
        String hukou = request.getParameter("hukou");
        String marriage = request.getParameter("marriage");

        //管理员修改员工信息新增 1.入行时间 2.小区ID 3.楼栋ID 4.房间ID 5.铺位ID 6.入职部门 7.密码 8.家庭是否深圳
        Date entrytime = DateUtil.strToDate(request.getParameter("entrytime"));

        //  1.各个id类型都为为Long， getParameter返回值若为null或“”则什么也不做 若改变了则会随之改变
        //  2.（数据库里为空值时 getParameter返回的是String的“null” 因此if里面用“null”来判断 “”同理 但是导致修改时无法删除该数据成空行）
        //        小区
        if ("null".equals(request.getParameter("estateId")) || "".equals(request.getParameter("estateId"))) {
        } else {
            Long estateId = Long.parseLong(request.getParameter("estateId"));
            staff.setEstateId(estateId);
        }
        //        楼栋
        if ("null".equals(request.getParameter("buildId")) || "".equals(request.getParameter("buildId"))) {
        } else {
            Long buildId = Long.parseLong(request.getParameter("buildId"));
            staff.setBuildId(buildId);
        }
        //        房间
        if ("null".equals(request.getParameter("roomId")) || "".equals(request.getParameter("roomId"))) {
        } else {
            Long roomId = Long.parseLong(request.getParameter("roomId"));
            staff.setRoomId(roomId);
        }
        //        铺位
        if ("null".equals(request.getParameter("bedId")) || "".equals(request.getParameter("bedId"))) {
        } else {
            Long bedId = Long.parseLong(request.getParameter("bedId"));
            staff.setBedId(bedId);
        }
        String department = request.getParameter("department");
        String password = request.getParameter("password");
        String family = request.getParameter("family");

        staff.setStaffId(staffId);
        staff.setStaffName(name);
        staff.setGender(gender);
        staff.setStaffPhone(String.valueOf(phone));
        staff.setEducation(education);
        staff.setIdentity(String.valueOf(identity));
        staff.setBankcard(String.valueOf(bankcard));
        staff.setStaffAddress(address);
        staff.setHukou(hukou);
        staff.setMarriage(marriage);

        //管理员修改员工信息新增 1.入行时间  6.入职部门 7.密码 8.家庭是否深圳
        staff.setEntrytime(entrytime);
        staff.setDepartment(department);
        staff.setStaffPassword(password);
        staff.setFamily(family);

        staffService.updateByIdSelective(staff);
        Staff newStaff = staffService.selectById(staff.getStaffId());
        session.setAttribute("user", newStaff);

        return "redirect:/success/updateStaffInfo_success.jsp";
    }
}
